package com.bua.control;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageButton;

public class IrregularButton extends ImageButton {

	public class TouchChecker {
		private Bitmap bitmap;

		public TouchChecker(Bitmap bitmap) {
			this.bitmap = bitmap;
		}

		boolean isInTouchArea(int x, int y, int width, int height) {
			if (bitmap != null && y<bitmap.getHeight() && x<bitmap.getWidth()) {
				int pixel = bitmap.getPixel(x, y);

				if (((pixel >> 24) & 0xff) > 0) {
					return true;
				}
			}

			return false;
		}

	}

	private TouchChecker touchChecker;

	public IrregularButton(Context context) {
		super(context);

		init();
	}

	public IrregularButton(Context context, AttributeSet attrs) {
		super(context, attrs);

		init();
	}

	public IrregularButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		init();
	}

	private void init() {
	}
	public void setImage(Bitmap bitmap)
	{
		super.setImageBitmap(bitmap);
		touchChecker=new TouchChecker(bitmap);
		
	}
	public void setTouchChecker(TouchChecker touchChecker) {
		this.touchChecker = touchChecker;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (touchChecker != null) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				if (touchChecker.isInTouchArea((int) event.getX(),
						(int) event.getY(), getWidth(), getHeight())) {
					return super.onTouchEvent(event);
				} else {
					return false;
				}
			}
		}

		return super.onTouchEvent(event);
	}
	

}
